package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.Property;
import cn.wolfcode.shop.service.ICatalogService;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.vo.JSONResultVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/property")
public class PropertyController {

    @Reference
    private ICatalogService catalogService;
    @Reference
    private IPropertyService propertyService;

    /**
     *显示商品分类,从redis缓存中拿
     * @param model
     * @return
     */
    @RequestMapping("")
    public String propertyPage(Model model){
        model.addAttribute("allCatalog",catalogService.selectAllByCache());
        return "property/property";
    }

    /**
     * 显示类别下的分类属性
     * @param catalogId
     * @param model
     * @return
     */
    @RequestMapping("/get/{id}")
    public String get(@PathVariable("id") Long catalogId, Model model){

        model.addAttribute("propertyList",propertyService.queryByCId(catalogId));
        return "property/property_list";
    }


    /**
     * 新增或编辑分类属性
     * @param property
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONResultVo saveOrUpdate(Property property){
        propertyService.saveOrUpdate(property);
        return JSONResultVo.instance();
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public JSONResultVo delete(@PathVariable("id") Long propertyId){

        propertyService.deleteByPId(propertyId);
        return JSONResultVo.instance();
    }


}
